package com.hnx.admin.aurora.core.api;

import com.hnx.admin.aurora.core.SqlWithParams;
import com.hnx.admin.aurora.utils.AnnReflectionUtil;
import com.hnx.admin.aurora.web.exception.ReflectionException;
import org.springframework.data.annotation.Id;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;

public interface BuildSqlStrategy {
    public final static String USER_ID = "userId";
    public final static String ID = "id";
    public static final String TABLE = "sys_base";
    public static final String DEPT_ID = "deptId";
    public static final String CREATE_BY = "createBy";
    public SqlWithParams process(Method method, Object obj);
    default Object getId(Object obj){
        List<Field> fieldList = AnnReflectionUtil.getAnnotationFields(obj.getClass(), Id.class);
        if(fieldList != null && fieldList.size() > 0) {
            Field field = fieldList.get(0);
            if (Objects.isNull(field)) {
                throw new ReflectionException();
            }
            field.setAccessible(true);
            try {
                return field.get(obj);
            } catch (Exception ex) {
                return null;
            }
        }
        return null;
    }
}
